﻿@model PsCommandDtls

@{
    ViewBag.Title = "คำสั่งบรรจุ";
    GERPContext db = new GERPContext(true);
    var CommandModel = db.PsCommands.Find(Model.CmdId);
}

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <h3 class="header"><span class="green">เพิ่ม</span> รายชื่อในคำสั่ง</h3>
    <div class="detail">
        <h3><label id="lblCmdTypeName"></label></h3>
        @Html.HiddenFor(model => model.CmdId)
        @Html.HiddenFor(model => model.CmdDtlsId)

        <table class="info">
            <tr class="left2">
                <td class="editor-label">
                    @Html.LabelFor(model => model.EmpId)
                </td>
                <td class="editor-field">
                    @(Html.Telerik().ComboBoxFor(m => m.EmpId)
                        .BindTo(new SelectList(new GERPContext(true).Employees
                            .OrderBy(m => m.FirstName)
                            //.OrderBy(m => m.LastName)
                            .Where(m => m.RecordStatus != "D" && m.MasEmployeeStatus.Active == "1" && m.EmpTypeId == CommandModel.EmpTypeId)
                            .ToList(), "EmpId", "FullName"))
                        //.SelectedIndex(Model != null ? (model.HrmPsCommandDtls.EmpId != null ? model.HrmPsCommandDtls.EmpId.Value : -1) : -1)
                        .HighlightFirstMatch(true)
                        .Filterable(filtering => filtering.FilterMode(AutoCompleteFilterMode.Contains))
                        .ClientEvents(client => client.OnChange("ddlEmpChange"))
                    )
                    @Html.ValidationMessageFor(model => model.EmpId)
                </td>
            </tr>
            <tr class="left">
                <td class="editor-label">
                    @Html.LabelFor(model => model.StartDate)
                </td>
                <td class="editor-field">
                    @Html.EditorFor(model => model.StartDate)
                    @Html.ValidationMessageFor(model => model.StartDate)
                </td>
            </tr>
            <tr class="left">
                <td class="editor-label">
                    @Html.LabelFor(model => model.Remarks)
                </td>
                <td class="editor-field">
                    @Html.EditorFor(model => model.Remarks)
                    @Html.ValidationMessageFor(model => model.Remarks)
                </td>
            </tr>
            <tr class="left2">
                <td class="editor-label">
                    @Html.LabelFor(model => model.RptRemarks)
                </td>
                <td class="editor-field">
                    @Html.EditorFor(model => model.RptRemarks)
                    @Html.ValidationMessageFor(model => model.RptRemarks)
                </td>
            </tr>
        </table>
        <table class="info">
            <tr>
                <th>

                </th>
                <th colspan="1">
                    ข้อมูลเดิม
                </th>
                <th colspan="1">
                    ข้อมูลใหม่
                </th>
            </tr>
            <tr>
                <td class="editor-label" width="150px">
                    ตำแหน่ง
                </td>
                <td class="editor-field" width="300px">
                    @Html.HiddenFor(model => model.OldPosId)
                    @Html.TextBoxFor(model => model.OldPosRef, new { @readonly="readonly", style="width:50px;" }) : 
                    @Html.TextBoxFor(model => model.OldPosName, new { @readonly="readonly", style="width:150px;" } )
                    @Html.ValidationMessageFor(model => model.OldPosId)
                </td>
                <td class="editor-field" width="300px">
                    @(Html.Telerik().ComboBoxFor(m => m.NewPosId)
                        .BindTo(new SelectList(new GERPContext(true).Positions
                            .Where(m => m.RecordStatus != "D" && m.EmpTypeId == CommandModel.EmpTypeId)
                            .ToList().OrderBy(m => m.PosNo), "PosId", "PosText"))
                        //.SelectedIndex(Model != null ? (Model.EmpTypeId != null ? Model.EmpTypeId.Value : -1) : -1)
                        .HighlightFirstMatch(true)
                        .Filterable(filtering => filtering.FilterMode(AutoCompleteFilterMode.Contains))
                        .ClientEvents(client => client.OnChange("ddlNewPosChange"))
                    )
                    <br/>@Html.HiddenFor(m => m.NewPosRef)
                    @Html.HiddenFor(m => m.NewPosName)
                        
                    @Html.ValidationMessageFor(model => model.NewPosId)
                </td>
            </tr>
            <tr>
                <td class="editor-label">
                    ระดับ
                </td>
                <td class="editor-field">
                    @Html.HiddenFor(model => model.OldClassId)
                    @Html.TextBoxFor(model => model.old_HrmMpClasses.ClassName, new { @readonly="readonly" })
                    @Html.ValidationMessageFor(model => model.OldClassId)
                </td>
                <td class="editor-field">
                    @(Html.Telerik().ComboBoxFor(m => m.NewClassId)
                        .BindTo(new SelectList(new GERPContext(true).MpClass
                            .Where(m => m.RecordStatus != "D" && m.EmpTypeId == CommandModel.EmpTypeId)
                            .ToList(), "ClassId", "ClassName"))
                        //.SelectedIndex(Model != null ? (Model.EmpTypeId != null ? Model.EmpTypeId.Value : -1) : -1)
                        .HighlightFirstMatch(true)
                        .Filterable(filtering => filtering.FilterMode(AutoCompleteFilterMode.Contains))
                        //.ClientEvents(client => client.OnChange("ddlNewClassChange"))
                    )
                        
                    @Html.ValidationMessageFor(model => model.NewClassId)
                </td>
            </tr>
            <tr>
                <td class="editor-label">
                    ตำแหน่งบริหาร
                </td>
                <td class="editor-field">
                    @Html.HiddenFor(model => model.OldExecPosId)
                    @Html.TextBoxFor(model => model.OldExecPosName, new { @readonly="readonly" })
                    @Html.ValidationMessageFor(model => model.OldExecPosName)
                </td>
                <td class="editor-field">
                    @(Html.Telerik().ComboBoxFor(m => m.NewExecPosId)
                          .BindTo(new SelectList(new GERPContext(true).ExecutivePositions
                                                     .Where(m => m.RecordStatus != "D")
                                                     .Distinct()
                                                     .ToList(), "ExecPosId", "Name"))
                          //.SelectedIndex(Model != null ? (Model.EmpTypeId != null ? Model.EmpTypeId.Value : -1) : -1)
                          .HighlightFirstMatch(true)
                          .Filterable(filtering => filtering.FilterMode(AutoCompleteFilterMode.Contains))
                          .ClientEvents(client => client.OnChange("ddlNewExecChange"))
                          )
                    <br/>
                    
                    @Html.HiddenFor(m => m.NewExecPosName)
                        
                    @Html.ValidationMessageFor(model => model.NewExecPosId)
                </td>
            </tr>
            <tr>
                <td class="editor-label">
                    สังกัด
                </td>
                <td class="editor-field">
                    @Html.HiddenFor(model => model.OldJobSiteId)
                    @Html.TextBoxFor(model => model.OldJobSiteName, new { @readonly="readonly" })
                    @Html.ValidationMessageFor(model => model.OldJobSiteId)
                </td>
                <td class="editor-field">
                    @(Html.Telerik().ComboBoxFor(m => m.NewJobSiteId)
                        .BindTo(new SelectList(new GERPContext(true).JobSites
                            .Where(m => m.RecordStatus != "D" && m.JobSiteTypeId == 1)
                            .OrderBy(m => m.Name)
                            .ToList(), "JobSiteId", "Name"))
                        //.SelectedIndex(Model != null ? (Model.EmpTypeId != null ? Model.EmpTypeId.Value : -1) : -1)
                        .HighlightFirstMatch(true)
                        .Filterable(filtering => filtering.FilterMode(AutoCompleteFilterMode.Contains))
                        .ClientEvents(client => client.OnChange("ddlNewJobSiteChange"))
                    )
                    <br/>@Html.HiddenFor(m => m.NewJobSiteName)
                        
                    @Html.ValidationMessageFor(model => model.NewJobSiteId)
                </td>
            </tr>
            <tr>
                <td class="editor-label">
                    สังกัดที่ปฏิบัติงาน
                </td>
                <td class="editor-field">
                    @Html.HiddenFor(model => model.OldWorkingJobSiteId)
                    @Html.TextBoxFor(model => model.OldWorkingJobSiteName, new { @readonly="readonly" })
                    @Html.ValidationMessageFor(model => model.OldWorkingJobSiteId)
                </td>
                <td class="editor-field">
                    @(Html.Telerik().ComboBoxFor(m => m.NewWorkingJobSiteId)
                        .BindTo(new SelectList(new GERPContext(true).JobSites
                            .Where(m => m.RecordStatus != "D" && m.JobSiteTypeId == 2)
                            .OrderBy(m => m.Name)
                            .ToList(), "JobSiteId", "Name"))
                        //.SelectedIndex(Model != null ? (Model.EmpTypeId != null ? Model.EmpTypeId.Value : -1) : -1)
                        .HighlightFirstMatch(true)
                        .Filterable(filtering => filtering.FilterMode(AutoCompleteFilterMode.Contains))
                        .ClientEvents(client => client.OnChange("ddlNewWorkingJobSiteChange"))
                    )
                    <br/>@Html.HiddenFor(m => m.NewWorkingJobSiteName)
                    @*<label id="lblNewWorkingJobSiteName" />*@
                        
                    @Html.ValidationMessageFor(model => model.NewWorkingJobSiteId)
                </td>
            </tr>
            <tr hidden visible="false">
                <td class="editor-label">
                    เงินเดือน
                </td>
                <td class="editor-field">
                    @Html.TextBoxFor(model => model.OldSalary, new { @readonly="readonly" })
                    @Html.ValidationMessageFor(model => model.OldSalary)
                </td>
                <td class="editor-field">
                    @Html.EditorFor(m=> m.NewSalary) บาท
                    @Html.ValidationMessageFor(model => model.NewSalary)
                </td>
            </tr>
        </table>            
    
        <p id="pnlCommand2">
            <input type="submit" value="บันทึก" />&nbsp;&nbsp;
            @Html.ActionLink("ยกเลิกการแก้ไข", "Edit", new { id = Model.CmdId })
        </p>
    </div>
}

@section Scripts {
    <script type="text/javascript" >
        jQuery(function ($) {
            $("input#approved_type").change(function () {
                $("#divApproverInternal").toggle(this.value == "1");
                $("#divApproverExternal").toggle(this.value == "2");
            });
            $("input#approved_type:checked").change();
        });

        function ddlPenaltyChange(e) {
            var comboBox = $("#PenaltyId").data("tComboBox");
            $("[id^='lblCmdTypeName']").text('โทษ>' + comboBox.text());
        }

        function ddlEmpChange(e) {
            $.getJSON('@Url.Action("GetEmployeeForCommandDetail", "Employee")', { 'id': e.value }, function (data) {
                try {
                    $("#OldPosId").val(data.pos_seq);
                    $("#OldPosRef").val(data.pos_ref);
                    $("#OldPosName").val(data.pos_name);
                    $("#OldClassId").val(data.class_seq);
                    $("#old_HrmMpClasses_ClassName").val(data.class_name);
                    $("#OldExecPosName").val(data.exec_pos_name);
                    $("#OldJobSiteId").val(data.job_site_seq);
                    $("#OldJobSiteName").val(data.job_site_name);
                    $("#OldWorkingJobSiteId").val(data.working_job_site_seq);
                    $("#OldWorkingJobSiteName").val(data.working_job_site_name);
                    $("#OldSalary").val(data.salary);
                } catch (v)
                {
                    alert(v);
                }
            });
        }

        function ddlNewPosChange(e) {
            var ddl = $("#NewPosId-input").val().split(" : ");
            $("#NewPosRef").val(ddl[0]);
            $("#NewPosName").val(ddl[1]);
        }
        //function ddlNewClassChange(e) {
            
        //    $("#new_class_name").val($("#NewClassId-input").val());
        //}
        function ddlNewExecChange(e) {
            $("#NewExecName").val($("#NewExecName-input").val());
        }
        function ddlNewJobSiteChange(e) {
            $("#NewJobSiteName").val($("#NewJobSiteId-input").val());
        }
        function ddlNewWorkingJobSiteChange(e) {
            $("#NewWorkingJobSiteName").val($("#NewWorkingJobSiteId-input").val());
        }

        @*jQuery(function ($) {
            $("#lnkAddDetail").click(function () {
                $("#pnlHeader").hide("slide", { direction: "left" }, 300, function () {

                    $.get('@(Url.Action("CreateDetail","CommandAdd", new { id = Model.CmdId } ))', function (data) {
                        $('#pnlDetailInfo').html(data);
                        $("#pnlDetailInfo").show("slide", { direction: "right" }, 300);
                    });

                    
                });
            });

            $("#pnlHeader").show("slide", { direction: "left" }, 300);
            $("#pnlDetailInfo").hide();
            
            $("[id^=lnkEditDetail]").click(function (event) {
                var url = '@Html.ActionLink("CommandAdd", "EditDetail")';

                var _id = event.target.id.replace("lnkEditDetail");

                url += "/" + _id;

                $.get(url, function (data) {
                    $('#pnlDetailInfo').replaceWith(data);
                });
                $("#pnlDetailInfo").show("slide", { direction: "right" }, 300);
                $.getJSON('@Url.Action("GetDetail", "CommandAdd")', { 'id': event.target.id.replace("lnkEditDetail", "") }, function (data) {


                    var CmdId = $("#CmdId");
                    var CmdDtlsId = $("#CmdDtlsId");
                    var EmpId = $("#EmpId");
                    var StartDate = $("#StartDate");
                    var EndDate = $("#EndDate");
                    var Remarks = $("#Remarks");
                    var RptRemarks = $("#RptRemarks");

                    CmdId.val(data.CmdId);
                    CmdDtlsId.val(data.CmdDtlsId);
                    EmpId.select(data.EmpId);
                    StartDate.val(data.StartDate);
                    EndDate.val(data.EndDate);
                    Remarks.val(data.Remarks);
                    RptRemarks.val(data.RptRemarks);

                });
                $("#pnlHeader").hide("slide", { direction: "left" }, 300, function () {
                    $("#pnlDetailInfo").show("slide", { direction: "right" }, 300)
                });
            });

            $("#lnkCancel").click(function () {

                $("#pnlDetailInfo").hide("slide", { direction: "right" }, 300, function () {
                    $("#pnlHeader").show("slide", { direction: "left" }, 300)
                });
            });
        });*@

        jQuery(function ($) {
            $("[type='submit']").click(function () {
                var ans = confirm("ต้องการบันทึก ใช่หรือไม่?");

                if (!ans) return false;
            });
        })

        jQuery(function ($) {
            $(".datepicker").datepicker({
                showOn: "focus",
            });
        });

        jQuery(function ($) {
            $("table.tablesorter").tablesorter({ widthFixed: true, sortList: [[0, 0]], widgets: ["zebra", "filter"] });
                //.tablesorterPager({ container: $("#pager"), size: $(".pagesize option:selected").val() });
        });
    </script>
}